<template>
    <el-form :model="regulationForm" :rules="regulationRules" ref="regulationForm" label-width="180px" size="mini">
        <el-form-item label="执行事件类型" prop="eventType">
            <el-select v-model="regulationForm.eventType" filterable>
                <el-option v-for="item in eventTypeOptions" :key="item.value" :label="item.label" :value="item.value">
                </el-option>
            </el-select>
        </el-form-item>
        <div v-show="regulationForm.eventType">
            <el-form-item label="执行表单" prop="executeForm">
                <el-select v-model="regulationForm.executeForm" filterable @change="()=>{regulationForm.filterCondition = 0,regulationForm.executeAction = 0}">
                    <el-option v-for="item in executeFormOptions" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="执行事件" prop="executeEvent">
                <el-select v-model="regulationForm.executeEvent" filterable>
                    <el-option v-for="item in executeEventOptions" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="过滤条件" prop="filterCondition">
                <el-select v-model="regulationForm.filterCondition" filterable>
                    <el-option v-for="item in filterConditionOptions[regulationForm.executeForm ? 'task' : 'workOrder']" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="执行动作" prop="executeAction">
                <el-select v-model="regulationForm.executeAction" filterable>
                    <el-option v-for="item in executeActionOptions[regulationForm.executeForm ? 'task' : 'workOrder']" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
        </div>
        <div v-show="!regulationForm.eventType">
            <el-form-item label="执行对象" prop="pushObject">
                <el-select v-model="regulationForm.pushObject" filterable>
                    <el-option v-for="item in pushObjectOptions" :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="提醒对象">
                <select-tree ref="tree" :instructType="instructType" :editInfo="editInfo" :isGetAllLabel="false"></select-tree>
            </el-form-item>
            <div class="messagePushWrapper__3LKkF">
                <div class="title__NIfjx">推送内容</div>
                <el-form-item label="标题" prop="title">
                    <el-input v-model="regulationForm.title" placeholder="请输入"></el-input>
                </el-form-item>
                <el-form-item label="工单号" prop="workOrderCode">
                    <el-input v-model="regulationForm.workOrderCode" placeholder="请输入"></el-input>
                </el-form-item>
                <el-form-item label="工序" prop="processName">
                    <el-input v-model="regulationForm.processName" placeholder="请输入"></el-input>
                </el-form-item>
                <el-form-item label="内容" prop="text">
                    <el-input v-model="regulationForm.text" placeholder="请输入"></el-input>
                </el-form-item>
                <el-form-item label="上报人" prop="user">
                    <el-input v-model="regulationForm.user" placeholder="请输入"></el-input>
                </el-form-item>
                <el-form-item label="推送内容" prop="msg">
                    <el-input type="textarea" v-model="regulationForm.msg" placeholder="请输入" :autosize="{ minRows: 5}">
                    </el-input>
                </el-form-item>
            </div>
        </div>
    </el-form>
</template>

<script>
    import selectTree from '@/components/selectTree'
    export default {
        data() {
            var validatorTitle = (rule, value, callback) => {
                if (!value && this.regulationForm.eventType == 0) {
                    return callback(new Error('标题不能为空!'));
                } else {
                    callback()
                }
            };
            var validatorCode = (rule, value, callback) => {
                if (!value && this.regulationForm.eventType == 0) {
                    return callback(new Error('工单号不能为空!'));
                } else {
                    callback()
                }
            };
            var validatorProcessName = (rule, value, callback) => {
                if (!value && this.regulationForm.eventType == 0) {
                    return callback(new Error('工序不能为空!'));
                } else {
                    callback()
                }
            };
            var validatorText = (rule, value, callback) => {
                if (!value && this.regulationForm.eventType == 0) {
                    return callback(new Error('内容不能为空!'));
                } else {
                    callback()
                }
            };
            var validatorUser = (rule, value, callback) => {
                if (!value && this.regulationForm.eventType == 0) {
                    return callback(new Error('上报人不能为空!'));
                } else {
                    callback()
                }
            };
            var validatorMsg = (rule, value, callback) => {
                if (!value && this.regulationForm.eventType == 0) {
                    return callback(new Error('推送内容不能为空!'));
                } else {
                    callback()
                }
            };
            return {
                regulationForm: {
                    eventType: 0,
                    pushObject: 0,
                    userId: '',
                    depId: '',
                    title: '',
                    executeForm:0,
                    executeEvent:0,
                    filterCondition:0,
                    executeAction:0,
                    workOrderCode: '',
                    processName: '',
                    text: '',
                    user: '',
                    msg: ''
                },
                eventTypeOptions: [{
                    value: 0,
                    label: '消息推送'
                }, {
                    value: 1,
                    label: '数据变更'
                }],
                pushObjectOptions: [{
                    value: 0,
                    label: '微信-消息通知'
                }],
                executeFormOptions: [{
                    value: 0,
                    label: '工单'
                },{
                    value: 1,
                    label: '任务'
                }],
                executeEventOptions: [{
                    value: 0,
                    label: '编辑数据'
                }],
                filterConditionOptions:{
                    workOrder:[{
                        value: 0,
                        label: '本工单'
                    }],
                    task:[{
                        value: 0,
                        label: '上一个任务'
                    },{
                        value: 1,
                        label: '下一个任务'
                    },{
                        value: 2,
                        label: '本道任务'
                    }]
                },
                executeActionOptions:{
                    workOrder:[{
                        value: 0,
                        label: '开始工单'
                    },{
                        value: 1,
                        label: '结束工单'
                    }],
                    task:[{
                        value: 0,
                        label: '开始任务'
                    },{
                        value: 1,
                        label: '结束任务'
                    }]
                },
                regulationRules: {
                    title: [{
                        required: true,
                        validator:validatorTitle,
                        trigger: 'change'
                    }],
                    workOrderCode: [{
                        required: true,
                        validator:validatorCode,
                        trigger: 'change'
                    }],
                    processName: [{
                        required: true,
                        validator:validatorProcessName,
                        trigger: 'change'
                    }],
                    text: [{
                        required: true,
                        validator:validatorText,
                        trigger: 'change'
                    }],
                    user: [{
                        required: true,
                        validator:validatorUser,
                        trigger: 'change'
                    }],
                    msg: [{
                        required: true,
                        validator:validatorMsg,
                        trigger: 'change'
                    }],
                    eventType: [{
                        required: true,
                    }],
                    pushObject: [{
                        required: true,
                    }],
                    executeForm: [{
                        required: true,
                    }],
                    executeEvent: [{
                        required: true,
                    }],
                    filterCondition: [{
                        required: true,
                    }],
                    executeAction: [{
                        required: true,
                    }],
                },
                editInfo:{
                    userId:'',
                    depId:'',
                    selectNames:''
                }
            }
        },
        props:{
            instructType:{
                type: String,
                default: 'create'
            }
        },
        components:{
            selectTree
        },
        methods: {
            getExecuteRuleInfo() {
                const {userId,depId} = this.$refs.tree.getTreeInfo()
                const {
                    eventType,pushObject,executeForm,executeEvent,filterCondition,
                    executeAction,title,workOrderCode,processName,text,user,msg
                } = this.regulationForm
                let data = {
                    eventType
                }
                if(eventType == 0) { //消息推送
                    data.pushObject = pushObject
                    data.userId = userId
                    data.depId = depId
                    data.missionPushMessVo = {
                        title,
                        workOrderCode,
                        processName,
                        text,
                        user,
                        msg
                    }
                }else if(eventType == 1){ //数据变更
                    data.executeForm = executeForm
                    data.executeEvent = executeEvent
                    data.filterCondition = filterCondition
                    data.executeAction = executeAction
                }
                return data
            },
            receiveProp(row) {
                const {eventType,pushObject,missionPushMessVo,executeForm,executeEvent,
                    filterCondition,executeAction,depId,userId,depNames,userNames} = row
                this.regulationForm.eventType = eventType
                if(eventType == 0){ //消息推送
                    this.regulationForm.pushObject = pushObject
                    this.regulationForm.title = missionPushMessVo.title
                    this.regulationForm.workOrderCode = missionPushMessVo.workOrderCode
                    this.regulationForm.processName = missionPushMessVo.processName
                    this.regulationForm.text = missionPushMessVo.text
                    this.regulationForm.user = missionPushMessVo.user
                    this.regulationForm.msg = missionPushMessVo.msg
                    this.editInfo.userId = userId
                    this.editInfo.depId = depId
                    this.editInfo.selectNames = this.utils.handleNames(userNames,depNames)
                }else if(eventType == 1){ //数据变更
                    this.regulationForm.executeForm = executeForm
                    this.regulationForm.executeEvent = executeEvent
                    this.regulationForm.filterCondition = filterCondition
                    this.regulationForm.executeAction = executeAction
                }
            },
            inspectForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$emit('handleConfirm')
                    } else {
                        return false;
                    }
                });
            },
            open(){
                this.$nextTick(()=>{
                    this.$refs.tree.handleDefaultValue(this.instructType)
                })
            },
            resetForm() {
                this.$refs.tree.clearTreeInfo()
                this.$refs.regulationForm.resetFields()
            }
        }
    }
</script>

<style lang="less" scoped>
    .messagePushWrapper__3LKkF {
        background-color: #e5e5e5;
        padding: 15px 0 15px;

        .title__NIfjx {
            font-size: 12px;
            padding-left: 15px;
        }
    }

    /deep/ .el-select {
        width: 80%;
    }

    .el-input,
    .el-textarea {
        width: 80%;
    }
</style>